package com.itbaizhan.shopping_user_customer_api.controller;

/**
 * @author spf
 * @date 2024/1/10
 * @time 13:29
 */

import com.itbaizhan.shopping_common.pojo.ShoppingUser;
import com.itbaizhan.shopping_common.result.BaseResult;
import com.itbaizhan.shopping_common.service.MessageService;
import com.itbaizhan.shopping_common.service.ShoppingUserService;
import com.itbaizhan.shopping_common.utils.RandomUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

/**
 * 前台用户API
 */
@RestController
@RequestMapping("/user/shoppingUser")
public class UserController {
    @Reference
    private ShoppingUserService userService;
    @Reference
    private MessageService messageService;

    /**
     * 发送注册短信
     * @param phone 手机号
     * @return 执行结果
     */
    @GetMapping("/sendMessage")
    public BaseResult sendMessage(String phone) {
        // 生成验证码
        String checkCode = RandomUtil.buildCheckCode(4);
        // 发送短信
        BaseResult baseResult = messageService.sendMessage(phone, checkCode);
        // 发送成功之后将手机号和验证码保存在redis中
        if (baseResult.getCode() == 200) {
            userService.saveRegisterCheckCode(phone, checkCode);
            return BaseResult.ok();
        } else {
            return baseResult;
        }
    }

    /**
     *  验证注册验证码
     * @param phone 手机号
     * @param checkCode 验证码
     * @return 执行结果
     */
    @GetMapping("/registerCheckCode")
    public BaseResult registerCheckCode(String phone, String checkCode) {
        userService.registerCheckCode(phone, checkCode);
        return BaseResult.ok();
    }

    /**
     * 注册
     * @param shoppingUser 商品用户对象
     * @return 执行结果
     */
    @PostMapping("/register")
    public BaseResult register(@RequestBody ShoppingUser shoppingUser) {
        userService.register(shoppingUser);
        return BaseResult.ok();
    }

    /**
     * 账户密码登录
     * @param shoppingUser 商品用户对象
     * @return 执行结果
     */
    @PostMapping("/loginPassword")
    public BaseResult<String> loginPassword(@RequestBody ShoppingUser shoppingUser){
        String token = userService.loginPassword(shoppingUser.getUsername(), shoppingUser.getPassword());
        return BaseResult.ok(token);
    }

    /**
     * 发送登录验证码
     * @param phone 手机号
     * @return 执行结果
     */
    @GetMapping("/sendLoginCheckCode")
    public BaseResult sendLoginCheckCode(String phone){
        String checkCode = RandomUtil.buildCheckCode(4);
        BaseResult result = messageService.sendMessage(phone, checkCode);
        if (result.getCode() == 200){
            userService.saveLoginCheckCode(phone, checkCode);
            return BaseResult.ok();
        }
        return result;
    }

    /**
     * 验证手机号验证码并登录
     * @param phone 手机号
     * @param checkCode 验证码
     * @return 执行结果
     */
    @PostMapping("/loginCheckCode")
    public BaseResult<String> loginCheckCode(String phone, String checkCode){
        String token = userService.loginCheckCode(phone, checkCode);
        return BaseResult.ok(token);
    }

    /**
     * 获取用户名
     * @param token 令牌
     * @return 执行结果
     */
    @GetMapping("/getName")
    public BaseResult<String> getName(@RequestHeader("token") String token){
        String name = userService.getName(token);
        return BaseResult.ok(name);
    }

}
